package com.wontube.smr.cron;

import java.util.HashSet;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.wontube.core.cron.IJobs;
import com.wontube.smr.bo.sites.crunchyroll.CrunchyrollObtainBO;

/**
 * @author tanggf(2012-8-3)
 *
 */
public class ObtainCrunchyrollJobs implements IJobs {
	private final static Logger logger = LoggerFactory.getLogger(ObtainCrunchyrollJobs.class);

	@Override
	public String name() {
		return "obtain-crunchyroll";
	}

	@Override
	public IJobs execute(String[] args) {
		Set<String> set = new HashSet<String>();
		for (String arg : args) {
			set.add(arg);
		}
		
		if(set.contains("-anime")){
			this.obtainAnimes();
		}
		
		if(set.contains("-anime_simulcasts")){
			this.obtainAnimeSimulcasts();
		}
		
		if(set.contains("-anime_recent")){
			this.obtainRecentAnimes();
		}
		
		if(set.contains("-anime_genres")){
			this.updateAnimeGenres();
		}
		
		if(set.contains("-drama")){
			this.obtainDramas();
		}
		
		if(set.contains("-drama_category")){
			this.updateDramaCategory();
		}
		
		return this;
	}
	
	private void obtainAnimes(){
		long start = System.currentTimeMillis();	
		int count = 0;
		try {
			count = CrunchyrollObtainBO.saveAllAnimes();
		} catch (Exception e) {
			logger.error("errors happen when grab Crunchyroll Animes: ", e);
		}
		long cost = System.currentTimeMillis() - start;
		cost /= 1000;
		logger.info("obtain {} Crunchyroll Animes, cost {} s", count, cost);		
	}
	
	private void obtainAnimeSimulcasts(){
		long start = System.currentTimeMillis();	
		int count = 0;
		try {
			count = CrunchyrollObtainBO.saveAnimeSimulcasts();
		} catch (Exception e) {
			logger.error("errors happen when grab Crunchyroll Anime Simulcasts: ", e);
		}
		long cost = System.currentTimeMillis() - start;
		cost /= 1000;
		logger.info("obtain {} Crunchyroll Anime simulcasts, cost {} s", count, cost);
	}
	
	private void obtainRecentAnimes(){
		long start = System.currentTimeMillis();	
		int count = 0;
		try {
			count = CrunchyrollObtainBO.saveRecentAnimes();
		} catch (Exception e) {
			logger.error("errors happen when grab Crunchyroll Anime recent updateds: ", e);
		}
		long cost = System.currentTimeMillis() - start;
		cost /= 1000;
		logger.info("obtain {} Crunchyroll Anime recent updateds, cost {} s", count, cost);
	}
	
	private void updateAnimeGenres(){
		long start = System.currentTimeMillis();	
		int count = 0;
		try {
			count = CrunchyrollObtainBO.updateAnimeGenres();
		} catch (Exception e) {
			logger.error("errors happen when update Crunchyroll Animes genres: ", e);
		}
		long cost = System.currentTimeMillis() - start;
		cost /= 1000;
		logger.info("update {} Crunchyroll Animes genres, cost {} s", count, cost);
	}
	
	private void obtainDramas(){
		long start = System.currentTimeMillis();	
		int count = 0;
		try {
			count = CrunchyrollObtainBO.saveAllDramas();
		} catch (Exception e) {
			logger.error("errors happen when grab Crunchyroll Dramas: ", e);
		}
		long cost = System.currentTimeMillis() - start;
		cost /= 1000;
		logger.info("obtain {} Crunchyroll Dramas, cost {} s", count, cost);	
	}
	
	private void updateDramaCategory(){
		long start = System.currentTimeMillis();	
		int count = 0;
		try {
			count = CrunchyrollObtainBO.updateDramaCountryCategory();
		} catch (Exception e) {
			logger.error("errors happen when update Crunchyroll Drama genres: ", e);
		}
		long cost = System.currentTimeMillis() - start;
		cost /= 1000;
		logger.info("update {} Crunchyroll Drama category, cost {} s", count, cost);	
	}
	
}
